write_latex = function(star_table, note_text, out_path) {
  table_note = note_text %>% 
    str_replace_all("\n", " ") %>%
    paste0("\\parbox[t]{\\wd\\tablebox}{", ., "}")
  out_table = append(star_table, 
                     "\\begin{lrbox}{\\tablebox}",
                     str_detect(star_table, "[\\\\]label\\{[0-9A-Za-z:_]+\\}") %>% which) %>%
    append(., 
           c("\\end{lrbox}","\\usebox{\\tablebox}\\\\[1ex]",table_note),
           str_detect(., "[\\\\]end\\{tabular\\}") %>% which)
  cat(out_table, sep = '\n', file = out_path)
}

digits = function(x, digits = 3) {
  re = paste0("0\\.\\d{",digits + 1,",}")
  fmt = paste0("%.", digits,"f") 
  patterns = x %>%  str_extract_all(re) %>% unlist  
  rounded = patterns %>% as.numeric %>% round(digits = digits) %>% 
    sprintf(fmt = fmt, .)
  names(rounded) = patterns
  str_replace_all(x, pattern =  rounded) %>% return
}